package com.task.src.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundTransUniTransferModel;
import com.alipay.api.domain.Participant;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.task.src.config.AlipayConfig;
import com.task.src.dto.Alipay;
import com.task.src.eum.Status;
import com.task.src.dto.User;
import com.task.src.mapper.UserMapper;
import com.task.src.service.Impl.UserServiceImpl;
import com.task.src.util.ResponseResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserServiceImpl userService;

    @Resource
    private UserMapper userMapper;

    @Resource
    HttpSession session;

    @RequestMapping("/register")
    public ResponseResult insertUser(User user,String code, HttpSession session){
        ResponseResult res=new ResponseResult();
        Object vCode = session.getAttribute("code");
        String uname=user.getUname();
        String psd=user.getPsd();
        String email=user.getEmail();
        if(vCode.equals(code)){
            int i=userService.insertUser(uname,psd,email);
            if(i!=0){
                res.setCode(200);
                res.setMessage(Status.REGISTER_SUCCESS);
                return res;
            }
            res.setCode(500);
            res.setMessage(Status.REGISTER_FAIL);
            return res;
        }
        res.setCode(500);
        res.setMessage(Status.CODE_WRONG);
        return res;
    }
    @RequestMapping("/login")
    public ResponseResult selectUser(String uname,String psd,HttpSession session){
        ResponseResult res=new ResponseResult();
        int i=userService.selectUser(uname,psd);
        if(i!=0){
            session.setAttribute("userid",i);
            res.setCode(200);
            res.setMessage(Status.LOGIN_SUCCESS);
            res.setData(i);
            return res;
        }
        res.setData(500);
        res.setMessage(Status.LOGIN_FAIL);
        return res;
    }

    @RequestMapping("/toMoney")
    public String toMyMoney(Alipay alipay) throws AlipayApiException {
        String s="WLKQTX";
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
        String s1=simpleDateFormat.format(date);
        ResponseResult res=new ResponseResult();
        AlipayClient  alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl,
                AlipayConfig.app_id,
                AlipayConfig.merchant_private_key,
                "json",
                AlipayConfig.charset,
                AlipayConfig.alipay_public_key,
                AlipayConfig.sign_type);
        AlipayFundTransUniTransferRequest request=new AlipayFundTransUniTransferRequest();
        AlipayFundTransUniTransferModel model=new AlipayFundTransUniTransferModel();
        model.setOutBizNo(s1+s);
        model.setOrderTitle("未来可期任务提现到账");
        model.setRemark("未来可期任务平台提现到账");
        model.setTransAmount(alipay.getAmount());
        model.setBizScene("DIRECT_TRANSFER");
        model.setProductCode("TRANS_ACCOUNT_NO_PWD");
        model.setBusinessParams("{凯凯:true}");
        Participant participant=new Participant();
        participant.setIdentity("2088622957044995");
        participant.setIdentityType("ALIPAY_USER_ID");
        model.setPayeeInfo(participant);
        request.setBizModel(model);
        AlipayFundTransUniTransferResponse response=alipayClient.execute(request);
        alipay.setId(s+s1);
        alipay.setPayee_account("2088622957044995");
        alipay.setPayee_type("支付宝账号转账");
        alipay.setPayer_show_name("未来可期任务提现");
        alipay.setPayee_real_name("kaix");
        alipay.setRemark("代发");
        System.out.println(response.getBody());
        if (response.isSuccess()) {
             return insertOrder(alipay);
        } else {
            return fail();
        }
    }


    public String insertOrder(Alipay alipay){
        userMapper.insertOrder(alipay);
        int amount=Integer.parseInt(alipay.getAmount());
        String uid = alipay.getUid();
        int id = Integer.parseInt(uid);
        User user = userMapper.selectMes(id);
        int yesTx=Integer.parseInt(user.getYesTx());
        int noTx=Integer.parseInt(user.getNoTx());
        String yes=yesTx+amount+"";
        String no=noTx-amount+"";
        System.out.println(yes+"    "+no);
        userMapper.updateUser(yes,no,uid);
        return "转账成功";
    }

    public String fail(){
        return "转账失败";
    }

    @RequestMapping("/mes")
    public ResponseResult selectMes(int id){
        ResponseResult res=new ResponseResult();
        User user=userMapper.selectMes(id);
        res.setData(user);
        return res;
    }

    @RequestMapping("/myTx")
    public ResponseResult selectMyTx(HttpSession session){
        int id=(int)session.getAttribute("userid");
        String uid=id+"";
        ResponseResult res=new ResponseResult();
        List<Alipay> alipay=userMapper.selectMyTx(uid);
        res.setData(alipay);
        return res;
    }

    @RequestMapping("/forgetPsd")
    public String forgetPsd(String psd,String email,String code){
        String code1 = (String) session.getAttribute("code");
        if(!code.equals(code1)){
            return "验证码不正确";
        }
        userMapper.updateUserPsd(email,psd);
        return "success";
    }
}
